/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sputty.classes;

import java.io.File;
import javax.swing.JTree;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import sputty.gui.ConnectionTreeRendering;

/**
 *
 * @author mauro
 */
public class ParseConfiguration extends Thread{
    
    JTree Tree;
    DefaultMutableTreeNode root;
    public ParseConfiguration(JTree Tree){
        this.Tree = Tree;
    }
    
    public void run(){
        try{
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse (new File(getClass().getResource("/sputty/configuration/connections.xml").getFile()));
            doc.getDocumentElement().normalize();
            Tree.setCellRenderer(new ConnectionTreeRendering());
            NodeList ConnTree = doc.getElementsByTagName("root");
            if(ConnTree.getLength() == 1){
                root = new DefaultMutableTreeNode(ConnTree.item(0).getAttributes().getNamedItem("name").getNodeValue());
                NodeList TreeContent = ((Element)ConnTree.item(0)).getChildNodes();
                ChildsExpand(TreeContent, root);
            }else{
                
            }
            ((DefaultTreeModel)Tree.getModel()).setRoot(root);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    
    private void ChildsExpand(NodeList NL, DefaultMutableTreeNode SubRoot){
        for(int i=0;i<NL.getLength();i++){
            if(NL.item(i).getAttributes() != null){
                if(NL.item(i).getAttributes().getNamedItem("type").getNodeValue().equals("folder")){
                    DefaultMutableTreeNode node = new DefaultMutableTreeNode(NL.item(i).getAttributes().getNamedItem("name").getNodeValue());
                    SubRoot.add(node);
                    ChildsExpand(((Element)NL.item(i)).getChildNodes(), node);
                }else{
                    try{
                        ConnectionSettings ConSet = new ConnectionSettings();
                        ConSet.setName(NL.item(i).getAttributes().getNamedItem("name").getNodeValue());
                        if(NL.item(i).hasChildNodes()){
                            Node Connection_info = ((Element)NL.item(i).getChildNodes()).getElementsByTagName("connection_info").item(0);
                            if(Connection_info instanceof Element){
                                if(((Element)Connection_info).getElementsByTagName("protocol").item(0) != null){
                                    ConSet.setProtocol(((Element)Connection_info).getElementsByTagName("protocol").item(0).getTextContent());
                                }
                                if(((Element)Connection_info).getElementsByTagName("host").item(0) != null){
                                    ConSet.setHost(((Element)Connection_info).getElementsByTagName("host").item(0).getTextContent());
                                }
                                if(((Element)Connection_info).getElementsByTagName("port").item(0) != null){
                                    ConSet.setPort(Integer.parseInt(((Element)Connection_info).getElementsByTagName("port").item(0).getTextContent()));
                                }
                            }
                            Node Login = ((Element)NL.item(i).getChildNodes()).getElementsByTagName("login").item(0);
                            if(Login instanceof Element){
                                if(((Element)Login).getElementsByTagName("login").item(0) != null){
                                    ConSet.setUserName(((Element)Login).getElementsByTagName("login").item(0).getTextContent());
                                }
                                if(((Element)Login).getElementsByTagName("password").item(0) != null){
                                    ConSet.setPassword(((Element)Login).getElementsByTagName("password").item(0).getTextContent());
                                }
                            }
                            Node timeout = ((Element)NL.item(i).getChildNodes()).getElementsByTagName("timeout").item(0);
                            if(timeout instanceof Element){
                                if(((Element)timeout).getElementsByTagName("connectiontimeout").item(0) != null){
                                    ConSet.setConnectionTimeout(Integer.parseInt(((Element)timeout).getElementsByTagName("connectiontimeout").item(0).getTextContent()));
                                }
                                if(((Element)timeout).getElementsByTagName("logintimeout").item(0) != null){
                                    ConSet.setLoginTimeOut(Integer.parseInt(((Element)timeout).getElementsByTagName("logintimeout").item(0).getTextContent()));
                                }
                                if(((Element)timeout).getElementsByTagName("passwordtimeout").item(0) != null){
                                    ConSet.setPasswordTimeOut(Integer.parseInt(((Element)timeout).getElementsByTagName("passwordtimeout").item(0).getTextContent()));
                                }
                                if(((Element)timeout).getElementsByTagName("commandtimeout").item(0) != null){
                                    ConSet.setCommandTimeOut(Integer.parseInt(((Element)timeout).getElementsByTagName("commandtimeout").item(0).getTextContent()));
                                }
                            }
                            Node command = ((Element)NL.item(i).getChildNodes()).getElementsByTagName("command").item(0);
                            if(command instanceof Element){
                                String[] Commands = new String[5];
                                if(((Element)command).getElementsByTagName("command1").item(0) != null){
                                    Commands[0] = (((Element)command).getElementsByTagName("command1").item(0).getTextContent());
                                }
                                if(((Element)command).getElementsByTagName("command2").item(0) != null){
                                    Commands[1] = (((Element)command).getElementsByTagName("command2").item(0).getTextContent());
                                }
                                if(((Element)command).getElementsByTagName("command3").item(0) != null){
                                    Commands[2] = (((Element)command).getElementsByTagName("command3").item(0).getTextContent());
                                }
                                if(((Element)command).getElementsByTagName("command4").item(0) != null){
                                    Commands[3] = (((Element)command).getElementsByTagName("command4").item(0).getTextContent());
                                }
                                if(((Element)command).getElementsByTagName("command5").item(0) != null){
                                    Commands[4] = (((Element)command).getElementsByTagName("command5").item(0).getTextContent());
                                }
                                ConSet.setCommands(Commands);
                            }
                            Node options = ((Element)NL.item(i).getChildNodes()).getElementsByTagName("options").item(0);
                            if(options instanceof Element){
                                if(((Element)options).getElementsByTagName("loginmacro").item(0) != null){
                                    ConSet.setLoginMacro(Boolean.parseBoolean(((Element)options).getElementsByTagName("loginmacro").item(0).getTextContent()));
                                }
                                if(((Element)options).getElementsByTagName("postcommands").item(0) != null){
                                    ConSet.setPostCommands(Boolean.parseBoolean(((Element)options).getElementsByTagName("postcommands").item(0).getTextContent()));
                                }
                                if(((Element)options).getElementsByTagName("endlinechar").item(0) != null){
                                    ConSet.setEndLineChar(Integer.parseInt(((Element)options).getElementsByTagName("endlinechar").item(0).getTextContent()));
                                }
                            }
                        }
                        DefaultMutableTreeNode node = new DefaultMutableTreeNode(ConSet);
                        SubRoot.add(node);
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }        
    }
    
}
